/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.framework.util.lotterydraw;

import java.util.Arrays;
import java.util.Random;

/**
 * 轮盘赌算法原理：给定List，元素值越大取到的概率越大（越小）
 *
 * @author liym
 * @since 2020-01-06 16:53 新建
 */
public final class RouletteTheory {

    /**
     * 给定一个正整数数组，随机取出一个数，要求数值越大的数被被取出的概率越大。
     *
     * <p>https://blog.csdn.net/Noedge/article/details/78282226</p>
     *
     * @param arr 数组
     * @return 取得的数据
     */
    public static int cheatGet(int[] arr) {
        Arrays.sort(arr);
        int sum = 0;
        for (int i = 1; i <= arr.length; i++) {
            sum += i;
        }
        int[] processedData = new int[sum];
        for (int i = 0, index = 0; i < processedData.length; index++, i += index) {
            for (int j = i; j < i + index + 1; j++) {
                processedData[j] = arr[index];
            }
        }
        System.out.println("已加工的数据：" + Arrays.toString(processedData));

        Random random = new Random();
        int hit = random.nextInt(sum);
        return processedData[hit];
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            System.out.println(cheatGet(new int[]{1, 2, 5, 10, 13}));
        }
    }

}
